package com.carassistant.beta.music.dialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.carassistant.beta.R;
import com.carassistant.beta.common.MyjamesTextView;

/**
 * @fMessageListAdapter.java 自定义对话框listview的适配器
 * @author thinkpad
 * @time2014-1-22
 * 
 */
public class MessageListAdapter extends BaseAdapter {

	private Context context;
	private List<Map<String, Object>> list;
	private LayoutInflater inflater;
	private ArrayList<MyjamesTextView> view;

	public MessageListAdapter(Context context, LayoutInflater layoutInflater,
			List<Map<String, Object>> list) {
		super();
		this.context = context;
		this.inflater = layoutInflater;
		this.list = list;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater
					.inflate(R.layout.message_listview_item, null);
			viewHolder = new ViewHolder();
			viewHolder.tv1 = (TextView) convertView
					.findViewById(R.id.textview1);
			// 将布局控件都放到view集合
			view.add((MyjamesTextView) viewHolder.tv1);
			viewHolder.tv2 = (TextView) convertView
					.findViewById(R.id.textview2);
			view.add((MyjamesTextView) viewHolder.tv2);
			convertView.setTag(viewHolder);
			viewHolder.tv1 = (TextView) convertView
					.findViewById(R.id.textview1);
			// 将布局控件都放到view集合
			view.add((MyjamesTextView) viewHolder.tv1);
			viewHolder.tv2 = (TextView) convertView
					.findViewById(R.id.textview2);
			view.add((MyjamesTextView) viewHolder.tv2);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 获取要显示的内容
		Map<String, Object> map = list.get(position);
		// 解析布局
		// XmlResourceParser xmlResourceParser =
		// context.getResources().getLayout(R.layout.message_listview_item);\
		// 遍历集合的key，根据key给对应控件赋值
		try {
			for (String key : map.keySet()) {
				for (int i = 0; i < view.size(); i++) {
					MyjamesTextView v = view.get(i);
					String dbfield = v.getField();
					if (dbfield.equals(key)) {
						String text = (String) map.get(key);
						v.setText(text);
					}
				}
			}
			
		} catch (Exception e) {
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv1;
		TextView tv2;
		TextView tv3;
		TextView tv4;
	}

}
